﻿using JESAI.Application.Abstractions.Dtos;
using JESAI.Application.Abstractions.Sys;
using JESAI.Application.Abstractions.Sys.Dtos;
using JESAI.Application.Abstractions.Sys.Querys;
using JESAI.AutoMapper.ObjectMapping;
using JESAI.Framework.Filters;
using JESAI.Platform.WebApi.Vos.Sys;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace JESAI.Platform.WebApi.Controllers.Sys
{
    [ApiController]
    [Route("api/v1/[controller]/[action]")]
    [Authorize("permission")]
    [IgnoreResultHandlerAttribute]
    public class DataSourceController : ControllerBase
    {
        private readonly IDataSourceService _dataSourceService;
        public DataSourceController(IDataSourceService dataSourceService)
        {
            _dataSourceService = dataSourceService;
        }

        /// <summary>
        /// 获取数据源表列表
        /// </summary>
        /// <param name="datasourceTableQuery"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<BasePageOutput<List<DataSourceTableResponse>>> GetPageList([FromQuery] DataSourceTablePageQuery datasourceTableQuery)
        {
            var formPageList = await _dataSourceService.GetPageList(datasourceTableQuery);
            return formPageList.MapTo<BasePageOutput<List<DataSourceTableResponse>>>();
        }

        /// <summary>
        /// 根据数据源表id查询数据源列
        /// </summary>
        /// <param name="dataSourceTableId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<BaseOutput> QueryDataSourceColumnsByTableId(string dataSourceTableId)
        {
            var list = await _dataSourceService.QueryDataSourceColumnsByTableId(dataSourceTableId);
            return new BaseOutput<List<DataSourceColumnResponse>>(list.MapTo<List<DataSourceColumnResponse>>());
        }

        /// <summary>
        /// 保存或更新数据源表
        /// </summary>
        /// <param name="dataSourceTableInput"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<BaseOutput> SaveOrUpdateDataSourceTable([FromBody] DataSourceTableInput dataSourceTableInput)
        {
            await _dataSourceService.SaveOrUpdateDataSourceTable(dataSourceTableInput);
            return new BaseOutput();
        }

        /// <summary>
        /// 保存或更新数据源
        /// </summary>
        /// <param name="dataSourceTableInput"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<BaseOutput> SaveOrUpdateDataSource([FromBody] DataSourceTableInput dataSourceTableInput)
        {
            await _dataSourceService.SaveOrUpdateDataSource(dataSourceTableInput, dataSourceTableInput.DataSourceColumnInputs);
            return new BaseOutput();
        }

        /// <summary>
        /// 删除数据源表
        /// </summary>
        /// <param name="dataSourceTableId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<BaseOutput> DelDataSourceTableById(string dataSourceTableId)
        {
            await _dataSourceService.DelDataSourceTableById(dataSourceTableId);
            return new BaseOutput();
        }

        /// <summary>
        /// 同步数据库
        /// </summary>
        /// <param name="dataSourceTableId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<BaseOutput> SyncDB(string dataSourceTableId)
        {
            await _dataSourceService.SyncDB(dataSourceTableId);
            return new BaseOutput();
        }

        /// <summary>
        /// 批量删除数据源表
        /// </summary>
        /// <param name="dataSourceTableIds"> 数据源表id数组 </param>
        /// <returns></returns>
        [HttpGet]
        public async Task<BaseOutput> DelDataSourceTableByIds(string[] dataSourceTableIds)
        {
            await _dataSourceService.DelDataSourceTableByIds(dataSourceTableIds);
            return new BaseOutput();
        }

        [HttpGet]
        public async Task<BaseOutput<List<DataSourceTableResponse>>> GetAllDataSourceTables()
        {
            var dataSourceTables = await _dataSourceService.GetAllDataSourceTables();
            return new BaseOutput<List<DataSourceTableResponse>>() { Data = dataSourceTables.MapTo<List<DataSourceTableResponse>>() };
        }

        [HttpGet]
        public async Task<BaseOutput<List<DataSourceColumnResponse>>> GetDataSourceColumnsByTableId(string tableId)
        {
            var dataSourceColumns = await _dataSourceService.GetDataSourceColumnsByTableId(tableId);
            return new BaseOutput<List<DataSourceColumnResponse>>() { Data = dataSourceColumns.MapTo<List<DataSourceColumnResponse>>() };
        }
    }
}
